Event.observe ( window, 'load', bootstrap );

function alter_go ( checkbox_ids ) {
  enable_or_disable ( 'product_go', checkbox_ids.find ( function ( id ) { return $( id ).checked } ) );
}

function reset () {
  disable_all ( $w( 'product_go svn_re_include svn_re_exclude svn_co_from_trunk do_svn_xls_file do_svn_mif_file' ) );
}

function bootstrap () {
  /*
  ** Go.
  */
  
  var checkbox_ids = $w( 'do_mifs do_spirits do_big_zip do_diagram do_abstract do_api do_verilog_pios do_tcl do_annotated_mifs do_pio_sheet do_followup' );
    
  alter_go ( checkbox_ids );
  
  /*
  ** Product.
  */
  
  var sub1 = disable_unless ( 'svn_product', $w( '       xls_file        mif_file' ), function ( node ) { return node.value === '-' } );
  var sub2 = disable_unless ( 'svn_product', $w( 'do_svn_xls_file do_svn_mif_file' ), function ( node ) { return node.value !== '-' } );
  
  var sub3 = function () {
    var product = $( 'svn_product' ).value;
    
    if ( product != '' && product != '-' ) {
      update_specification_branches ( product, 'svn_branch' );
      update_specification_tags     ( product, 'svn_tag'    );
    }
  };
  
  $( 'svn_product' ).observe ( 'change', function () { sub1 () ; sub2 () ; sub3 () } );
  
  /*
  ** Outputs.
  */
  
  var link_checkboxes_h = $H({
    'do_mifs'           : [ $w( ''                              ), '',               'followup'  ], /* #10 */
    'do_spirits'        : [ $w( 'do_mifs'                       ), '',               'followup'  ], /* #11 */
    'do_big_zip'        : [ $w( 'do_mifs'                       ), '',               'followup'  ], /* #12 */
    'do_diagram'        : [ $w( ''                              ), 'specification', ''           ], /* #13 */
    'do_abstract'       : [ $w( 'do_mifs do_spirits do_diagram' ), 'specification', '[followup]' ], /* #14 */
    'do_api'            : [ $w( 'do_mifs do_spirits'            ), 'specification', '[followup]' ], /* #15 */
    'do_verilog_pios'   : [ $w( ''                              ), 'specification', ''           ], /* #16 */
    'do_tcl'            : [ $w( ''                              ), 'specification', ''           ], /* #17 */
    'do_annotated_mifs' : [ $w( 'do_mifs do_spirits'            ), 'specification',  'followup'  ], /* #18 */
    'do_pio_sheet'      : [ $w( ''                              ), 'specification', ''           ], /* #19 */
    'do_followup'       : [ $w( ''                              ), 'specification', ''           ]  /* #20 */
  });
  
  link_checkboxes_h.each ( function ( pair ) {
    var master_id             = pair.key;
    var slave_ids             = pair.value[0];
    var needs_a_specification = pair.value[1] === 'specification';
    var needs_a_followup      = pair.value[2] === 'followup';
    var likes_a_followup      = pair.value[2] === '[followup]';
    
    var pre_requesites_ok = function () { return true };
    
    if      ( needs_a_specification && needs_a_followup ) {
      pre_requesites_ok = function () { if ( ! ( has_a_specification () && has_a_followup () ) ) { alert ( 'Requires a specification (MIF: #0 + [2] or #2) and a follow-up (XLS: #0 + [1] or #1)!'          ) ; return false } return true };
    }
    else if ( needs_a_specification && likes_a_followup ) {
      pre_requesites_ok = function () { if ( ! ( has_a_specification ()                      ) ) { alert ( 'Requires a specification (MIF: #0 + [2] or #2) and possibly a follow-up (XLS: #0 + [1] or #1)!' ) ; return false } return true };
    }
    else if ( needs_a_specification ) {
      pre_requesites_ok = function () { if ( ! ( has_a_specification ()                      ) ) { alert ( 'Requires a specification (MIF: #0 + [2] or #2)!'                                                ) ; return false } return true };
    }
    else if ( needs_a_followup ) {
      pre_requesites_ok = function () { if ( ! (                           has_a_followup () ) ) { alert ( 'Requires a follow-up (XLS: #0 + [1] or #1)!'                                                    ) ; return false } return true };
    }
    
    $( master_id ).observe ( 'click', function () { link_checkboxes ( master_id, slave_ids, pre_requesites_ok ) ; alter_go ( checkbox_ids ) } );
  } );
}

function has_a_specification () {
  return $( 'mif_file' ).value !== '' || ( $( 'svn_product' ).value !== '-' && $( 'do_svn_mif_file' ).checked );
}

function has_a_followup () {
  return $( 'xls_file' ).value !== '' || ( $( 'svn_product' ).value !== '-' && $( 'do_svn_xls_file' ).checked );
}
